package UIFree.controls
{
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class uif_searchList extends uif_list {
		public static const FILTER: String = 'FILTER';
		public static const FILTERED: String = 'FILTERED';
		
		protected static const INPUTHEIGHT: int = 30;
		 
		protected var _input: uif_input;
		protected var _oList: Array;
		protected var _cFunc: Function;
		
		public function uif_searchList(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			_input = createInputField();
			addEvent(Event.CHANGE, doInput, _input.textField);
		}
		
		protected function doInput(e: Event): void {
			if (_cFunc != null) {
				function isItem(a_element:*, a_index:int, a_arr:Array):Boolean {
					var event: ObjectEvent = new ObjectEvent(FILTER, 
						{
							element : a_element,
							index	: a_index,
							arr		: a_arr,
							word	: _input.text
						}
					); 
					dispatchEvent(event);
					return (event.result);
				}
				var newList: Array = _oList.filter(isItem);
				clearList();
				super.attachList(newList, _cFunc);
				dispatchEvent(new ObjectEvent(FILTERED, list));
			}
		}
		
		public function input(): uif_input {
			return _input;
		}
		
		override protected function createAcrollArea(): uif_scrollArea {
			var inn: Rectangle = getInner();
			return new uif_scrollArea(this, inn.x, inn.y + INPUTHEIGHT, inn.width - buttonSize(), inn.height - INPUTHEIGHT, 'box');
		}
		
		protected function createInputField(): uif_input {
			var inn: Rectangle = getInner();
			return new uif_input(this, inn.x, inn.y, inn.width, INPUTHEIGHT, stParam('inputClass', 'input')); 
		}
		
 		override public function attachList(list: Array, createFunc: Function=null): uif_list {
 			_oList = [].concat(list);
 			_cFunc = createFunc;
 			return super.attachList(list, createFunc);
 		}
	}
}